<style>
    .model-form .layui-form-label {
        width: 110px;
    }

    .model-form .layui-input-block {
        margin-left: 140px;
    }

    .cardU {
        background-image: url(../../../admin/assets/images/card_u.png);
        background-repeat: no-repeat;
        background-position: center;
        width: calc(100% - 142px);
        height: 150px;
        padding: 0;
    }

    .cardU img {
        width: 100%;
        height: 100%;
    }
</style>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">车牌号：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="plateNo" class="layui-input" type="text"
                                               placeholder="请输入车牌号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit>查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                            <div class="query-rt"></div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="vehicleBtnAdd" class="layui-btn icon-btn color-reseda">添加</button>
                        </div>
                        <div class="lay-btn-rt">
<!--                            <button id="vehicleBtnExport" class="layui-btn icon-btn layui-btn-danger">导出</button>-->
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="vehicleTable" lay-filter="vehicleTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="vehicleTableBar">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="vehicleForm">
    <form lay-filter="vehicleForm" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <input name="id" type="hidden"/>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red">*</span>车牌号</label>
                        <div class="layui-input-block">
                            <input name="plateNo" placeholder="请输入车牌号" type="text" class="layui-input"
                                   lay-verify="required|isOnly" required/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red">*</span>皮重</label>
                        <div class="layui-input-block">
                            <input name="tare" placeholder="请输入皮重" type="text" class="layui-input"
                                   lay-verify="required|decimal" required/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red">*</span>载重</label>
                        <div class="layui-input-block">
                            <input name="loadHeavy" placeholder="请输入载重" type="text" class="layui-input"
                                   lay-verify="required|decimal" required/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red">*</span>是否加长车</label>
                        <div class="layui-input-block">
                            <select name="isLong">
                                <option value="0">否</option>
                                <option value="1">是</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red">*</span>排放标准</label>
                        <div class="layui-input-block">
                            <input name="emissionStandards" placeholder="请输入排放标准" type="text" class="layui-input"
                                   lay-verify="required" required/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span style="color: red">*</span>是否启用</label>
                        <div class="layui-input-block">
                            <input name="isDeleted" type="checkbox" lay-skin="switch" value="1" checked="false"
                                   lay-text="启用|停用"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">车头照片</label>
                        <div class="layui-upload-drag cardU" id="frontPhoto">
                            <!--<i class="layui-icon"></i>
                            <p>点击上传，或将文件拖拽到此处</p>-->
                            <input type="hidden" name="frontPhoto" lay-verify="" data-name="车头照片">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">车尾照片</label>
                        <div class="layui-upload-drag cardU" id="rearPhoto">
                            <!--<i class="layui-icon"></i>
                            <p>点击上传，或将文件拖拽到此处</p>-->
                            <input type="hidden" name="rearPhoto" lay-verify="" data-name="车尾照片">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">侧面照片</label>
                        <div class="layui-upload-drag cardU" id="profilePhoto">
                            <!--<i class="layui-icon"></i>
                            <p>点击上传，或将文件拖拽到此处</p>-->
                            <input type="hidden" name="profilePhoto" lay-verify="" data-name="侧面照片">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">年检证</label>
                        <div class="layui-upload-drag cardU" id="documents">
                            <!--<i class="layui-icon"></i>
                            <p>点击上传，或将文件拖拽到此处</p>-->
                            <input type="hidden" name="documents" lay-verify="" data-name="年检证">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">其他证件</label>
                        <div class="layui-upload-drag cardU" id="otherDocuments">
                            <!--<i class="layui-icon"></i>
                            <p>点击上传，或将文件拖拽到此处</p>-->
                            <input type="hidden" name="otherDocuments" lay-verify="" data-name="其他证件">
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea name="remark" placeholder="请输入备注" maxlength="200" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="vehicleFormSubmit" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        // 渲染表格
        var insTb = table.render({
            elem: '#vehicleTable',
            url: config.base + 'enterprise/invoicing/admin/vehicle/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', width: 80}
                , {field: 'plateNo', align: 'center', title: '车牌号'}
                , {field: 'tare', align: 'center', title: '皮重'}
                , {field: 'loadHeavy', align: 'center', title: '载重'}
                , {
                    field: 'isLong', align: 'center', title: '是否加长车', templet: function (d) {
                        return d.isLong ? "是" : "否";
                    }
                }
                , {field: 'emissionStandards', align: 'center', title: '排放标准'}
                , {field: 'createTime', align: 'center', title: '创建时间'}
                , {align: 'center', toolbar: '#vehicleTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if (menuButton.indexOf("搜索") == -1) {
                    $('.layui-form.toolbar').remove();
                }
                if (menuButton.indexOf("修改") == -1) {
                    $("[lay-event='edit']").remove();
                }
                if (menuButton.indexOf("删除") == -1) {
                    $("[lay-event='del']").remove();
                }
            }
        });

        // 工具条点击事件
        table.on('tool(vehicleTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(vehicleTable)', function (obj) {
            table.reload('vehicleTable', {
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-query-box").find('select').val("");
            $(".lay-query-box").find('input').val("");
            var field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        // 添加按钮点击事件
        $('#vehicleBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: ['60%', '90%'],
                title: data ? '修改车辆' : '添加车辆',
                content: $('#vehicleForm').html(),
                success: function () {
                    //自定义验证规则
                    form.verify({
                        decimal: [/^[0-9]*(\.[0-9]{1,2})?$/, '格式错误'],
                        isOnly: function (value) {
                            var isOnly = true;
                            var idVal = $("form input[name='id']").val();
                            admin.req('mes/admin/salesBasisVehicle/isOnly', {code: value, id: idVal}, function (res) {
                                if (res.code == 200) {
                                    isOnly = res.data;
                                }
                            }, 'GET', false);
                            if (!isOnly) {
                                return "车牌号必须唯一"
                            }
                        }
                    })

                    if (data) {
                        var frontPhoto = data.frontPhoto;
                        if (frontPhoto) {
                            $("#frontPhoto").html('<img src="' + frontPhoto + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="frontPhoto" value="' + frontPhoto + '">');
                        }
                        var rearPhoto = data.rearPhoto;
                        if (rearPhoto) {
                            $("#rearPhoto").html('<img src="' + rearPhoto + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="rearPhoto" value="' + rearPhoto + '">');
                        }
                        var profilePhoto = data.profilePhoto;
                        if (profilePhoto) {
                            $("#profilePhoto").html('<img src="' + profilePhoto + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="profilePhoto" value="' + profilePhoto + '">');
                        }
                        var documents = data.documents;
                        if (documents) {
                            $("#documents").html('<img src="' + documents + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="documents" value="' + documents + '">');
                        }
                        var otherDocuments = data.otherDocuments;
                        if (otherDocuments) {
                            $("#otherDocuments").html('<img src="' + otherDocuments + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="otherDocuments" value="' + otherDocuments + '">');
                        }
                    }

                    //拖拽上传车头照片
                    upload.render({
                        elem: '#frontPhoto'
                        , url: config.upload_url + 'upload?path=' + img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        , headers: {authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            if (res.code == 200) {
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl = res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#frontPhoto").html('<img src="' + imagUrl + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="frontPhoto" value="' + imagUrl + '">');
                            } else if (res.code == 502) {
                                return location.replace('admin/login.html');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    //拖拽上传车尾照片
                    upload.render({
                        elem: '#rearPhoto'
                        , url: config.upload_url + 'upload?path=' + img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        , headers: {authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            if (res.code == 200) {
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl = res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#rearPhoto").html('<img src="' + imagUrl + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="rearPhoto" value="' + imagUrl + '">');
                            } else if (res.code == 502) {
                                return location.replace('admin/login.html');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    //拖拽上传侧面照片
                    upload.render({
                        elem: '#profilePhoto'
                        , url: config.upload_url + 'upload?path=' + img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        , headers: {authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            if (res.code == 200) {
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl = res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#profilePhoto").html('<img src="' + imagUrl + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="profilePhoto" value="' + imagUrl + '">');
                            } else if (res.code == 502) {
                                return location.replace('admin/login.html');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    //拖拽上传年检证
                    upload.render({
                        elem: '#documents'
                        , url: config.upload_url + 'upload?path=' + img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        , headers: {authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            if (res.code == 200) {
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl = res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#documents").html('<img src="' + imagUrl + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="documents" value="' + imagUrl + '">');
                            } else if (res.code == 502) {
                                return location.replace('admin/login.html');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    //拖拽上传其他证件
                    upload.render({
                        elem: '#otherDocuments'
                        , url: config.upload_url + 'upload?path=' + img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        , headers: {authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            if (res.code == 200) {
                                layer.msg("上传成功", {icon: 1});
                                var imagUrl = res.data.url;
                                /*if(imagUrl){
                                    imagUrl=imagUrl.substring(imagUrl.indexOf('/')+1)
                                }*/
                                $("#otherDocuments").html('<img src="' + imagUrl + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="otherDocuments" value="' + imagUrl + '">');
                            } else if (res.code == 502) {
                                return location.replace('admin/login.html');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }
                    });

                    form.val('vehicleForm', data);

                    // 表单提交事件
                    form.on('submit(vehicleFormSubmit)', function (d) {
                        layer.load(2);
                        var isDeleted = $("form input[name='isDeleted']").prop("checked");
                        if (!isDeleted) {
                            d.field.isDeleted = false;
                        } else {
                            d.field.isDeleted = true;
                        }
                        admin.req(data ? 'enterprise/invoicing/admin/vehicle/update' : 'enterprise/invoicing/admin/vehicle/add', d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('vehicleTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/invoicing/admin/vehicle/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#vehicleBtnExport').click(function () {
            var checkRows = table.checkStatus('vehicleTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>